import Vue from 'vue'
import Router from 'vue-router'

import AAA from '@/components/AAAScreen/AAA' // 抽油烟机大屏
import map from '@/components/AAAScreen/charts/BaiduMap.vue'
import Login from '@/components/Login/Login.vue'
import { isLogin } from '../api/api'

// ljm - big screen
import LJM from "@/components/LJM"

Vue.use(Router)

const router = new Router({
    routes: [
        {
            path: '/',
            component: LJM,
            meta:{
                title:"超高性能的海量存储服务"
            }
        },
        {
            path: '/login',
            name: 'login',
            component: Login
        },
        {
            path: '/aaa',
            name: 'aaa',
            component: AAA
        },
        {
            path: '/map',
            name: 'map',
            component: map
        },
        {
            path: "/ljm",
            name: "ljm",
            component: LJM,
            meta: {
                title: '超高性能的海量存储服务'
            }
        }
    ]
})

function min (n) {   //设置token过期时间
    return n * 60 * 1000
}

router.beforeEach(({ meta, path, query }, from, next) => {

    // // 1- 从localStorage获取token
    // const token = Boolean(localStorage.getItem('token'))

    // // 2- 从localStorage获取token的存储时间
    // let oldTime = localStorage.getItem('tokenTime') || new Date().getTime()

    // // 3- 判断token存储时间是否过期
    // let expired = new Date().getTime() - oldTime > min(60)

    // if (expired) {
    //     localStorage.clear()
    //     localStorage.setItem('tokenTime', new Date().getTime())
    //     if (path != '/login') {
    //         alert('登陆过期')
    //     }
    // }

    // if (token && !expired) {
    //     if (path === '/login') {
    //         next('/')
    //     }
    //     if (path === '/') {
    //         next()
    //     }
    // } else {
    //     if (path === '/login') {
    //         next()
    //     } else {
    //         next('/login')
    //     }
    // }
    // next()

    /*
        isLogin()
            .then(res => {
                let isExpired = res.data.code != 2000 || res.data.is_login == false;
    
                if (isExpired) {
                    // localStorage.clear()
                    // localStorage.setItem('tokenTime', new Date().getTime())
                    if (path != '/login') {
                        alert('登陆过期')
                    }
                }
    
                if (!isExpired) {
                    if (path === '/login') {
                        next('/')
                    }
                    if (path === '/') {
                        next()
                    }
                } else {
                    if (path === '/login') {
                        next()
                    } else {
                        next('/login')
                    }
                }
                next()
            })
            .catch(err => {
                console.log(err);
            })
    
        */

    if (meta.title) {
        document.title = meta.title
    }
    next()
})

export default router
